import { CurrentCityApi } from "@/apis/home.js"
//获取定位城市的数据
export const getCurrentCity = () => {
    //从本地存储中获取定位的城市信息
    const localCity = JSON.parse(localStorage.getItem("hkzf_city"))
    if (!localCity) {//如果本地不存在定位的城市信息重新从接口获取
        return new Promise((resolve, reject) => {
            try {
                //获取ip定位
                fetch(`https://restapi.amap.com/v3/ip?key=9926298cea7dcd3cf9a4710f2dc6b1fd`)
                .then(respone => respone.json())
                .then(async res => {
                    const result = await CurrentCityApi(res.city)
                    //并且存储到本地
                    localStorage.setItem("hkzf_city", JSON.stringify(result.body))
                    resolve(result.bod)
                })
            } catch (error) {
                reject(error)
            }
        })

    } else {//如果存在
        return Promise.resolve(localCity)
    }
}